package com.qeado.baike.web;

import java.io.File;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.qeado.baike.model.Baike;
import com.qeado.baike.model.BaikeRef;
import com.qeado.baike.service.BaikeService;
import com.qeado.user.model.User;
import com.qeado.util.FileUpload;
import com.qeado.util.PageHelper;
@Scope("session")
@Controller
public class BaikeAction extends ActionSupport {

	@Autowired
	private BaikeService baikeService;
	
	private PageHelper baikeList = null ; 
	private PageHelper newBaike = null; // 最新发布的百科信息
	
	private String baikeId = null; // 百科ID
	private String baikeWord = null; // 百科词条名称
	private Baike baike = null; // 百科实体类
	private BaikeRef baikeRef = null ; // 百科审查实体
	private int opt = 1; // 操作标志 1：创建 2：编辑 
	private String pageNum = "1";
	private String pageSize = "10";
	private String orderBy = "create_date"; // 默认按发布时间来排列
	private String asc = "desc"; // 默认是逆序
	private String typeCode = null ; // 默认分类
	
    private File myFile; // 上传的文件
    private String contentType; // 上传的文件类型
    private String fileName; // 上传文件的文件名

	public String getTypeCode() {
		return typeCode;
	}

	public void setTypeCode(String typeCode) {
		this.typeCode = typeCode;
	}

	public String getPageNum() {
		return pageNum;
	}

	public void setPageNum(String pageNum) {
		this.pageNum = pageNum;
	}

	public String getPageSize() {
		return pageSize;
	}

	public void setPageSize(String pageSize) {
		this.pageSize = pageSize;
	}

	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public String getAsc() {
		return asc;
	}

	public void setAsc(String asc) {
		this.asc = asc;
	}

	public PageHelper getBaikeList() {
		return baikeList;
	}

	public void setBaikeList(PageHelper baikeList) {
		this.baikeList = baikeList;
	}

	public void setMyFileContentType(String contentType) {
        this.contentType = contentType;
   } 
   
    public void setMyFileFileName(String fileName) {
        this.fileName = fileName;
   } 
       
    public void setMyFile(File myFile) {
        this.myFile = myFile;
   } 
    
	public BaikeRef getBaikeRef() {
		return baikeRef;
	}

	public void setBaikeRef(BaikeRef baikeRef) {
		this.baikeRef = baikeRef;
	}

	public void setBaike(Baike baike) {
		this.baike = baike;
	}

	public String getBaikeWord() {
		return baikeWord;
	}

	public void setBaikeWord(String baikeWord) {
		this.baikeWord = baikeWord;
	}

	public int getOpt() {
		return opt;
	}

	public void setOpt(int opt) {
		this.opt = opt;
	}

	public Baike getBaike() {
		return baike;
	}

	public String getBaikeId() {
		return baikeId;
	}

	public void setBaikeId(String baikeId) {
		this.baikeId = baikeId;
	}
	
	/**
	 * 首页
	 */
	public String execute() throws Exception {
		newBaike = baikeService.listAll(1, 2, "create_date", "desc");
		return super.execute();
	}
	
	/**
	 * 通过指定的ID查百科记录
	 * @return
	 */
	public String getBaikeById() {
		if(baikeId == null) {
			return "visit_fail";
		}		
		baike = baikeService.queryById(baikeId);		
		if(baike == null) {
			return "visit_fail";
		}
		
		return "view";
	}
	
	/**
	 * 通过制定类别查询百科词条
	 * @return
	 */
	public String getBaikeBySort() {
		if (null == typeCode ) {
			typeCode = "";
		}
		try {
			baikeList = baikeService.listBySort(Integer.parseInt(pageNum), Integer.parseInt(pageSize), typeCode, orderBy, asc);
			baikeList.setPagebar("${ctx}/baike/listBySort?typeCode=" + typeCode);
		} catch (Exception e) {
			return "visit_fail";
		}
		return "list";
	}
	
	/**
	 * 检查词条是否已经创建
	 * @return
	 */
	public String checkTerm() {
		if( baikeWord == null)
			return "visit_fail";
		baike = baikeService.queryByWord(baikeWord);
		if(baike == null) {
			opt = 1; // 如果词条不在数据库中，则创建词条
			baike = new Baike();
			baike.setBaikeWord(baikeWord);
		} else {
			opt = 2; // 如果词条已经存在数据库中，则查出来，并编辑
		}
		return "create";
	}
	
	/**
	 * 确保读取已存在的词条
	 * @return
	 */
	public String edit() {
		if(baikeId == null) {
			return "visit_fail";
		}
		if(baike == null) {
			baike = baikeService.queryById(baikeId);
		}		
		opt = 2;
		return "view";
	}
	
	/**
	 * 创建新词条
	 * @return
	 */
	public String create() {
		User user = (User) ActionContext.getContext().getSession().get(
		"login_user");
		FileUpload upload = null;
		if( myFile != null ){
			upload = new FileUpload();
			upload.setBasePath( ServletActionContext.getServletContext().getRealPath( "uploadfiles" ) );
			upload.setUserDir(user.getId());
			upload.setMyFile(myFile);
			upload.setFileName(fileName);
		}
		baike.setCreator(user.getUserName());
		baike.setModifier(user.getUserName());
		if (baikeService.createBaike(baike, upload) ) {
			baikeId = baike.getId();
			return getBaikeById();
		} else {
			return "visit_fail";
		}		
	}

	/**
	 * 按关键字查询词条
	 * @return
	 */
	public String searchBar() {
		String key = ServletActionContext.getRequest().getParameter("key");
		if( null == key ){
			key = " " ;
		}
		try {
			baikeList = baikeService.listByKey(Integer.parseInt(pageNum), Integer.parseInt(pageSize), key, orderBy, asc);
			baikeList.setPagebar("${ctx}/baike/search?key="+key);
		} catch (Exception e) {
			return "visit_fail";
		}
		return "list";
	}

	public PageHelper getNewBaike() {
		return newBaike;
	}

	public void setNewBaike(PageHelper newBaike) {
		this.newBaike = newBaike;
	}
	
}
